package com.tadpole.music.view.fragment.me;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lixs.charts.BarChart.LBarChartView;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.base.HomePagerAdapter;
import com.tadpole.music.adapter.home.DayAdapter;
import com.tadpole.music.adapter.home.WeekAdapter;
import com.tadpole.music.adapter.me.MemberCenterAdapter;
import com.tadpole.music.adapter.me.V2DayAdapter;
import com.tadpole.music.adapter.me.WorkV2Adapter;
import com.tadpole.music.adapter.me.ZhenTiWorkAdapter;
import com.tadpole.music.bean.home.ClockInBean;
import com.tadpole.music.bean.me.ZhenTiSearchBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.me.ZhenTiSearchIView;
import com.tadpole.music.presenter.me.ZhenTiSearchPresenter;
import com.tadpole.music.utils.DateUtils;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.me.StudentErrorActivity;
import com.tadpole.music.view.activity.me.ZhenTiStudentErrorActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;
import com.tadpole.music.view.widget.NoScrollViewPager;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.cache.Sp;

/**
 * 真题实战
 * Created By 孙岩
 * on 2020/12/25
 */
public class ZhenTiV2Fragment extends BaseFragment implements ZhenTiSearchIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    //rank_id
    private String mParam1;
    //学生id
    private String mParam2;
    //做题总数
    private TextView tvOne;
    //做题总时长
    private TextView tvTwo;
    //错题总数
    private TextView tvThree;
    //正确率
    private TextView tvFour;

    //总做题数
    private TextView tvOne1;
    //做题总时长
    private TextView tvTwo1;
    //总错题数
    private TextView tvThree1;
    //正确率
    private TextView tvFour1;
    //柱状图
    private LBarChartView frameNewBase1;

    //时间
    //上一月
    private View leftIcon;
    //下一月
    private TextView tvDate;
    private View rightIcon;

    private RadioGroup radioGroup;
    private RadioButton mRadio21;
    private RadioButton mRadio22;
    private RadioButton mRadio23;
    private RadioButton mRadio24;

    //更多数量
    private TextView tvMore;

    //筛选统计 有数据
    private LinearLayout llHaveData;
    //筛选统计 无数据
    private TextView tvNo;
    //星期数据
    private List<String> weekList;
    //星期recyclerView
    private RecyclerView rvWeek;
    //星期adapter
    private WeekAdapter weekAdapter;

    //天recyclerView
    private RecyclerView rvDay;
    //天adapter
    private V2DayAdapter dayAdapter;
    //天数的数据
    private List<ClockInBean> dayList;
    //获取真题数据的presenter
    private ZhenTiSearchPresenter zhenTiSearchPresenter;
    //实战真题 错题列表
    private RecyclerView rvWork;
    private ZhenTiWorkAdapter zhenTiWorkAdapter;
    //当前为0 向左为负数 向右为正数
    private int position = 0;

    public static ZhenTiV2Fragment newInstance(String param1, String param2) {
        ZhenTiV2Fragment fragment = new ZhenTiV2Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_zhenti_v2;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initView(View mRootView) {
        tvOne = mRootView.findViewById(R.id.tvOne);
        tvTwo = mRootView.findViewById(R.id.tvTwo);
        tvThree = mRootView.findViewById(R.id.tvThree);
        tvFour = mRootView.findViewById(R.id.tvFour);
        tvOne1 = mRootView.findViewById(R.id.tvOne1);
        tvTwo1 = mRootView.findViewById(R.id.tvTwo1);
        tvThree1 = mRootView.findViewById(R.id.tvThree1);
        tvFour1 = mRootView.findViewById(R.id.tvFour1);
        frameNewBase1 = mRootView.findViewById(R.id.frameNewBase1);
        rvDay = mRootView.findViewById(R.id.rvDay);
        rvWeek = mRootView.findViewById(R.id.rvWeek);
        rvWork = mRootView.findViewById(R.id.rvWork);
        leftIcon = mRootView.findViewById(R.id.leftIcon);
        rightIcon = mRootView.findViewById(R.id.rightIcon);
        tvDate = mRootView.findViewById(R.id.tvDate);
        radioGroup = mRootView.findViewById(R.id.radioGroup);

        llHaveData = mRootView.findViewById(R.id.llHaveData);
        tvNo = mRootView.findViewById(R.id.tvNo);
        mRadio21 = mRootView.findViewById(R.id.mRadio21);
        mRadio22 = mRootView.findViewById(R.id.mRadio22);
        mRadio23 = mRootView.findViewById(R.id.mRadio23);
        mRadio24 = mRootView.findViewById(R.id.mRadio24);
        tvMore = mRootView.findViewById(R.id.tvMore);

        initWeekData();
        initListeners();

        zhenTiSearchPresenter = new ZhenTiSearchPresenter();
        zhenTiSearchPresenter.attachView(this);
        zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, "", "", "1");
    }

    //初始化星期
    private void initWeekData() {
        //星期数据
        tvDate.setText(DateUtils.getCurrentYearAndMonth());
        //星期数据
        weekList = new ArrayList<>();
        weekList.add("日");
        weekList.add("一");
        weekList.add("二");
        weekList.add("三");
        weekList.add("四");
        weekList.add("五");
        weekList.add("六");

        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 7);
        rvWeek.setLayoutManager(layoutManager);
        //创建适配器
        weekAdapter = new WeekAdapter(getActivity(), weekList);
        //给RecyclerView设置适配器
        rvWeek.setAdapter(weekAdapter);
        rvWeek.setFocusable(false);
    }

    //初始化监听器
    private void initListeners() {
        //上个月
        leftIcon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        try {
                            //每次向左则-1
                            position = position - 1;
                            zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, DateUtils.getStartTime(position), DateUtils.getEndTime(position), "2");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        //下个月
        rightIcon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        try {
                            //每次向右则+1
                            position = position + 1;
                            zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, DateUtils.getStartTime(position), DateUtils.getEndTime(position), "2");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });

        //当日统计
        mRadio21.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        try {
                            mRadio21.setChecked(true);
                            String startDayTime = DateUtils.getStartDayTime(position, DateUtils.getDay());
                            String endDayTime = DateUtils.getEndDayTime(position, DateUtils.getDay());
                            zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, startDayTime, endDayTime, "3");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });

        //本周统计
        mRadio22.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        mRadio22.setChecked(true);
                        String startWeekTime = DateUtils.getWeekStart();
                        String endWeekTime = DateUtils.getWeekEnd();
                        zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, startWeekTime, endWeekTime, "3");
                    }
                } else {
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });

        //本月统计
        mRadio23.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        try {
                            mRadio23.setChecked(true);
                            String startMonth = DateUtils.getStartTime(0);
                            String endMonth = DateUtils.getEndTime(0);
                            zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, startMonth, endMonth, "3");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });

        //全部统计
        mRadio24.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, "2020-01-01 00:00:00", Mutils.getSystemTime(), "3");
                    }
                } else {
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });

        //更多
        tvMore.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), ZhenTiStudentErrorActivity.class);
                        intent.putExtra("rank_id", mParam1);
                        intent.putExtra("user_id", mParam2);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });
    }

    //设置导航栏为都不选中
    private void setNavFalse() {
        radioGroup.clearCheck();
    }

    //数据回调
    @SuppressLint("SetTextI18n")
    @Override
    public void showZhenTi(ZhenTiSearchBean.DataBean bean, String flag) {
        if (flag.equals("1")) {
            //总体统计
            tvOne.setText("做题套数" + bean.getTotal().getTotal() + "套");
            tvTwo.setText("平均时长" + BigDecimal.valueOf(bean.getTotal().getAvg_time()).setScale(0, BigDecimal.ROUND_UP).doubleValue() + "分钟");
            tvThree.setText("错题数" + bean.getTotal().getSum_err_num() + "道");
            tvFour.setText("平均分数" + bean.getTotal().getAvg_score() + "分");
//            new BigDecimal().setScale(2,BigDecimal.ROUND_UP).doubleValue()
            //柱形图
            final List<Double> datas = new ArrayList<>();
            final List<String> description = new ArrayList<>();
            datas.add(Double.parseDouble(bean.getTotal().getTotal() + ""));
            datas.add(BigDecimal.valueOf(bean.getTotal().getAvg_time()).setScale(0, BigDecimal.ROUND_UP).doubleValue());
            datas.add(Double.parseDouble(bean.getTotal().getSum_err_num() + ""));
            datas.add(Double.parseDouble(bean.getTotal().getAvg_score() + ""));
            description.add("总做题(套)");
            description.add("平均时长(分钟)");
            description.add("错题数(道)");
            description.add("平均分数(分)");
            frameNewBase1.setDatas(datas, description, true);
            setNavFalse();
        } else if (flag.equals("2")) {
            setNavFalse();
        }

        //天的数据
        showDayData(bean);
        //设置底部数据
        showBottomData(bean);
    }

    //设置天的数据
    private void showDayData(ZhenTiSearchBean.DataBean bean) {
        //设置日期
        tvDate.setText(DateUtils.getLeftMonth(position));
        int leftMaxDay = DateUtils.getLeftMonthLastDay(position);
        //天数据
        dayList = new ArrayList<>();

        for (int i = 0; i < DateUtils.getWeek(position) - 1; i++) {
            //DateUtil.getFirstDayOfMonth()获取当月第一天是星期几，星期日是第一天，依次类推 0为空 false签到状态为false
            dayList.add(new ClockInBean(0, false, false));
        }

        for (int i = 0; i < leftMaxDay; i++) {
            dayList.add(new ClockInBean(i + 1, false));
        }

        for (int x = 0; x < bean.getMonth_data().getDays().size(); x++) {
            for (int a = 0; a < dayList.size(); a++) {
                if (bean.getMonth_data().getDays().get(x) == dayList.get(a).getDay()) {
                    dayList.get(a).setClock(true);
                }
            }
        }

        for (int i = 0; i < dayList.size(); i++) {
            if (dayList.get(i).getDay() > DateUtils.getDay()) {
                if (position == 0) {
                    dayList.get(i).setNext(true);
                } else {
                    dayList.get(i).setNext(false);
                }
            }
        }

        GridLayoutManager layoutManager1 = new GridLayoutManager(getActivity(), 7);
        rvDay.setLayoutManager(layoutManager1);
        //创建适配器
        dayAdapter = new V2DayAdapter(getActivity(), dayList, position);
        //给RecyclerView设置适配器
        rvDay.setAdapter(dayAdapter);
        rvDay.setNestedScrollingEnabled(false);
        rvDay.setFocusable(false);

        dayAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int p) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        try {
                            String startDayTime = DateUtils.getStartDayTime(position, dayList.get(p).getDay());
                            String endDayTime = DateUtils.getEndDayTime(position, dayList.get(p).getDay());
                            zhenTiSearchPresenter.getZhenTi(mParam2, mParam1, startDayTime, endDayTime, "2");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    //设置底部数据
    @SuppressLint("SetTextI18n")
    private void showBottomData(final ZhenTiSearchBean.DataBean bean) {
        if (bean.getMonth_data_static().getTotal() == 0) {
            llHaveData.setVisibility(View.GONE);
            tvNo.setVisibility(View.VISIBLE);
        } else {
            llHaveData.setVisibility(View.VISIBLE);
            tvNo.setVisibility(View.GONE);
            //筛选统计
            tvOne1.setText("做题套数" + bean.getMonth_data_static().getTotal() + "套");
            tvTwo1.setText("平均时长" + BigDecimal.valueOf(bean.getMonth_data_static().getAvg_time()).setScale(0, BigDecimal.ROUND_UP).doubleValue() + "分钟");
            tvThree1.setText("错题数" + bean.getMonth_data_static().getSum_err_num() + "道");
            tvFour1.setText("平均分数" + bean.getMonth_data_static().getAvg_score() + "分");
        }

        //列表数据
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        rvWork.setLayoutManager(layoutManager2);
        //创建适配器
        zhenTiWorkAdapter = new ZhenTiWorkAdapter(getActivity(), bean);
        //给RecyclerView设置适配器
        rvWork.setAdapter(zhenTiWorkAdapter);
        rvWork.setFocusable(false);

        zhenTiWorkAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), StudentErrorActivity.class);
                        intent.putExtra("id", bean.getMonth_exam_static().get(position).getId());
                        intent.putExtra("flag", 2);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }
}
